package cn.itcast.bassm.service;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import cn.itcast.bassm.service.InputandUpdowService;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cn.itcast.bassm.util.readTxT;

public class MyServlet   extends HttpServlet {
	private static Logger mLogger = Logger.getLogger("addAlarmCallBak");
	
	private static String txtPath ="/usr/local/tomcat/tomcat8/webapps/configSEX.txt";
	private static String TimeSex=readTxT.readTxtLine(txtPath, 1);
	private static String alarmCallbackServer =TimeSex+"/api/v1/alarm/httpCallback";
	
	@Autowired
	private InputandUpdowService inputandUpdowService;
	
	
	@Override
	public void init() throws ServletException {
		super.init();
		final int interval = 60 * 1000;
		String msg =inputandUpdowService.getUser();
		while (msg == null) {
			String msgs=inputandUpdowService.getUser();
			if(msgs==null){
		try {
			Thread.sleep(interval);
			}
         catch (InterruptedException e) {
            System.out.println("程序出现问题了");
        }
		
	}
	
/*	public void addAlarmCallBakTo(){	
			boolean ret=false;	
			 while (!ret) {
				 
		            ret = addAlarmCallBak(TimeSex+"/bassm/user/savemsg");
		            try {
		                Thread.sleep(10000);
		            } catch (InterruptedException e) {
		                mLogger.error("InterruptedException" + e.toString());
		            }
		        }
	}

	private boolean addAlarmCallBak(String calbackurl) {
		 List<NameValuePair> params = new ArrayList<NameValuePair>();
	        params.add(new BasicNameValuePair("url", ""+calbackurl));
	        params.add(new BasicNameValuePair("enable", "true"));
	        params.add(new BasicNameValuePair("alarmOnly", "false"));
	        
	        boolean ret =  false;
	        String json = doBasicAuthPost(alarmCallbackServer, params);
	        if (json != null) {
	            JsonParser parser = new JsonParser(); // 创建JSON解析器
	            JsonObject object = (JsonObject) parser.parse(json); // 创建JsonObject对象
	            int errorcode = object.get("errorCode").getAsInt();
	            if (errorcode == 0) {
	                ret = true;
	            }
	            mLogger.info(json);
	        } else {
	            mLogger.error("connect addAlarmCallBak error");
	        }
	        
	        return ret;
	}
	
	public String doBasicAuthPost(String urlStr, List<NameValuePair> params ){
			String result = null;
	        HttpPost httpPost = new HttpPost(urlStr);

	        try {
	            httpPost.addHeader("Authorization", getHeader("admin","123456"));
	            HttpEntity entity = new UrlEncodedFormEntity(params, "UTF-8");// 设置字符集
	            httpPost.setEntity(entity);// 设置参数实体
	            @SuppressWarnings("resource")
	            HttpClient httpClient = new DefaultHttpClient();// 获取HttpClient对象
	            httpClient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, 30000);// 连接超时
	            httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT, 30000);// 请求超时

	            HttpResponse httpResp = httpClient.execute(httpPost);// 获取HttpResponse实例
	            if (httpResp.getStatusLine().getStatusCode() == 200) {// 判断是够请求成功
	                result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");// 获取返回的数据
	            } else {
	                mLogger.info("doBasicAuthPost getStatusCode " + httpResp.getStatusLine().getStatusCode());
	            }
	        } catch (ConnectTimeoutException e) {
	            mLogger.info("doBasicAuthPost ConnectTimeoutException " + e.toString());
	        } catch (UnsupportedEncodingException e) {
	            mLogger.info("doBasicAuthPost UnsupportedEncodingException " + e.toString());
	        } catch (IOException e) {
	            mLogger.info("doBasicAuthPost IOException " + e.toString());
	        } catch (Exception e) {
	            mLogger.info("doBasicAuthPost Exception " + e.toString());
	        }

	        return result;
	}
    *//**
     * 构造Basic Auth认证头信息
     * 
     * @return
     *//*
    private String getHeader(String username, String password) {
        String auth = username + ":" + password;
        byte[] encodedAuth = Base64.encodeBase64(auth.getBytes(Charset.forName("US-ASCII")));
        String authHeader = "Basic " + new String(encodedAuth);
        return authHeader;
    }

}
	}*/



}
	}
}
